package com.android.rdc.librarysystem.ui;

import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import com.android.rdc.amdroidutil.base.BaseToolbarActivity;
import com.android.rdc.amdroidutil.listener.OnClickRecyclerViewListener;
import com.android.rdc.librarysystem.API.net.BaseModel;
import com.android.rdc.librarysystem.API.net.ServerException;
import com.android.rdc.librarysystem.API.util.ToastUtils;
import com.android.rdc.librarysystem.R;
import com.android.rdc.librarysystem.adapter.BookRankRvAdapter;
import com.android.rdc.librarysystem.adapter.BookRvAdapter;
import com.android.rdc.librarysystem.bean.Book;
import com.android.rdc.librarysystem.bean.Rank;
import com.android.rdc.librarysystem.dao.RecordDao;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class BookRankActivity extends BaseToolbarActivity {
    private static final String TAG = "BookRankActivity";
    @BindView(R.id.rv)
    RecyclerView mRv;

    private BookRankRvAdapter mRvAdapter;
    private SearchView mSearchView;
    private RecordDao mRecordDao;
    private List<String> mRecordList = new ArrayList<>();
    private ArrayAdapter<String> mArrayAdapter;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    private void initAutoCompleteTv() {
        updateRecordList();
        mArrayAdapter = new ArrayAdapter<>(this, R.layout.item_record, mRecordList);
        AutoCompleteTextView autoCompleteTextView = mSearchView.findViewById(R.id.search_src_text);
        autoCompleteTextView.setAdapter(mArrayAdapter);
        autoCompleteTextView.setThreshold(0);
        autoCompleteTextView.setDropDownBackgroundResource(R.color.white);
        autoCompleteTextView.setOnItemClickListener((parent, view, position, id) -> {//这里需要自行设置 OnItemClickListener ，因为SearchView 中设置的监听器需要 CursorAdapter 而这里使用 的是 ArrayAdapter
            if (position == mRecordList.size() - 1) {//若点击的是底部项，删除所有记录
                mRecordDao.deleteAllRecord();
                //更新数据
                updateRecordList();
                mArrayAdapter.clear();//清空数据
            } else {
                mSearchView.setQuery(mRecordList.get(position), true);
            }
        });
    }

    private void updateRecordList() {
        mRecordList.clear();
        mRecordList.addAll(mRecordDao.queryRecord());//查询记录列表
        if (!mRecordList.isEmpty()) {
            mRecordList.add("清除所有记录");
        }
    }

    @Override
    protected int setLayoutResID() {
        return R.layout.activity_book_rank;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        setTitle("图书排行榜");
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        //List<Book> bookList = DataSupport.findAll(Book.class);//默认显示所有的书籍
        new BaseModel().rank(this)
                .subscribe(new Observer<List<Rank>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<Rank> list) {
                        System.out.println("获取排行榜成功");
                        updateBookList(list);
                        ToastUtils.toast(BookRankActivity.this, "获取排行榜成功");
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ServerException) {
                            ServerException se = (ServerException) e;
                            ToastUtils.toast(BookRankActivity.this, se.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }

    private void updateBookList(List<Rank> bookList) {
        if (mRvAdapter == null) {
            mRvAdapter = new BookRankRvAdapter();
            mRvAdapter.updateData(bookList);
            mRv.setLayoutManager(new LinearLayoutManager(this));
            mRv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
            mRv.setAdapter(mRvAdapter);
        } else {
            mRvAdapter.updateData(bookList);
            mRvAdapter.notifyDataSetChanged();
        }
    }
}
